﻿using BSAppDOne.WebAdmin.Custom;
using Models;
using Models.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BSAppDOne.WebAdmin.Areas.Admin.Controllers
{
    //所有权限控制的必须继续这个基类
    [RouteArea("Admin")]
    [AdminActionFilter]
    public class BaseController : Controller
    {
        /// <summary>
        /// 当前用户信息
        /// </summary>
        public Account _Account { get; set; } = new Account();

        /// <summary>
        /// 菜单组
        /// </summary>
        public List<Sys_Operation> _Menus { get; set; }


        [NonAction]
        public ActionResult Success()
        {
            return new JsonResult() { Data = new { status = 1 } };
        }
        [NonAction]
        public ActionResult Success(object data,string msg)
        {
            return new JsonResult() { Data = new { status = 1, data = data,msg=msg } };
        }

        [NonAction]
        public ActionResult Success(object data)
        {
            return new JsonResult() { Data = new { status=1,data=data} };
        }

        [NonAction]
        public ActionResult Error()
        {
            return new JsonResult() { Data = new { status = 0 } };
        }

        [NonAction]
        public ActionResult Error(object data, string msg)
        {
            return new JsonResult() { Data = new { status = 0, msg = msg, data = data } };
        }
        [NonAction]
        public ActionResult Error(string msg)
        {
            return new JsonResult() { Data = new { status = 0,msg=msg} };
        }

        /// <summary>
        /// 返回分部视图html
        /// </summary>
        /// <param name="viewName"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public string PartialViewToString(string viewName, object model=null)
        {
            IView view = ViewEngines.Engines.FindPartialView(this.ControllerContext, viewName).View;

            if(model!=null)
            {
                this.ViewData.Model = model;
            }           
            using (System.IO.StringWriter writer = new System.IO.StringWriter())
            {
                ViewContext viewContext = new ViewContext(this.ControllerContext, view, this.ViewData, this.TempData, writer);
                view.Render(viewContext, writer);
                return writer.ToString();
            }
        }

    }

    //jq datatable插件用的实体model
    public class TableBsEntity<T>
    {
        /// <summary>
        /// 请求次数（前端==》后端）
        /// </summary>
        public int draw { get; set; }

        /// <summary>
        /// 总记录数（前端《==后端）
        /// </summary>
        public int recordsTotal { get; set; }

        /// <summary>
        /// 过滤后的总记录数（前端《==后端）
        /// </summary>
        public int recordsFiltered { get; set; }

        /// <summary>
        /// 记录开始索引（前端==》后端）
        /// </summary>
        public int start { get; set; }

        /// <summary>
        /// PageSize（前端==》后端）
        /// </summary>
        public int length { get; set; }

        /// <summary>
        /// 集合分页数据（前端《==后端）
        /// </summary>
        public List<T> data { get; set; }
    }

}